<#include "/admin/public.html">
<div class="uk-layui-form">
	<form class="layui-form uk-form" action="/topic/adminupdate.html"  method="post">
	  <input type="hidden" name="id" value="${topic.id!''}">
	  <input type="hidden" name="topicFiles" >
	  <input type="hidden" name="deleteTopicFiles" >
	  <input type="hidden" name="essence" value="${topic.essence!''}">
	  <input type="hidden" name="hot" value="${topic.hot!''}">
	  <input type="hidden" name="top" value="${topic.top!''}">
	  <div class="layui-form-item hide">
            <label class="layui-form-label">选择板块</label>
            <div class="layui-input-inline">
           		<select name="cateparent" id="selectBlock" placeholder="请选择" lay-filter="selectBlock"  required lay-verify="required">
					<option value="">请选择</option>
					<#if blockList?? && blockList??>
					<#list blockList as list>
						<#if  list.type=="板块">
							<option value="${list.id!''}" <#if topic.cateparent == list.id> selected </#if> >${list.blockname!''}</option>
						</#if>
					</#list>
					</#if>
				</select>
            </div>
       </div>
       <div class="layui-form-item hide">
            <label class="layui-form-label">选择分类</label>
            <div class="layui-input-inline">
                <select name="cate" id="selectCategory" placeholder="请选择"  required lay-verify="required">
                	<option value="">请选择</option>
				</select>
            </div>
      </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label">标题</label>
	    <div class="layui-input-inline">
	      <input type="text" name="title"  value="${topic.title!''}" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
	    </div>
	  </div>
	   <div class="layui-form-item">
	    <label class="layui-form-label">浏览量</label>
	    <div class="layui-input-inline">
	      <input type="text" name="views"  value="${topic.views!''}" required lay-verify="required" placeholder="请输入浏览量" autocomplete="off" class="layui-input">
	    </div>
	  </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label">正文</label>
	    <div class="layui-input-block">
	     <textarea id="content" name="content"  lay-verify="content" placeholder="请输入正文" style="height: 260px;">${topic.content!''}</textarea>
	    </div>
	  </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label">附件上传</label>
	    <div class="layui-input-inline">
	    	<a class="layui-btn layui-btn-primary" id="informationUploadButton"><i class="layui-icon">&#xe608;</i>新增文件</a>
	    	<label style="color:red;">附件最多5个</label>
	    </div>
	  </div>
	  <div class="layui-form-item" id="inforShow">
	    <label class="layui-form-label"></label>
	     <div class="layui-input-block" id="showAttachment">
	     	<#if topicFiles??>
				<#list topicFiles as list>
					<li id="${list.id!''}">
		    			<span class="filename layui-elip">${list.enclosurename!''}</span>
		    			<a href="#" class="delete"><i class="layui-icon" onclick="deleteA(this,'${list.enclosureid!''}','${list.id!''}')">&#x1006;</i></a>
		    		</li>
				</#list>
			</#if>
	    </div>
	  </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label">标签</label>
	    <div class="layui-input-inline">
	     <input type="checkbox" name="topshow" title="置顶" <#if topic.top == "true">checked</#if>>
	    </div>
	  </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label">是否热帖</label>
	    <div class="layui-input-inline">
	     <input type="checkbox" name="hotshow" title="热帖"  <#if topic.hot == "true">checked</#if>>
	    </div>
	  </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label">是否精华帖</label>
	    <div class="layui-input-inline">
	     <input type="checkbox" name="essenceshow" title="精华"  <#if topic.essence == "true">checked</#if>>
	    </div>
	  </div>
	  <div class="layui-form-button">
	    <div class="layui-button-block">
	      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
	      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
	    </div>
	  </div>
	</form>
</div>
 
<script>

var listCategory = [], AttachmentList = [],deleteList = [];
var form, index, layedit;
var url = "/topicupload";
<#list topicFiles as list>
AttachmentList.push({"id":"${list.id!''}","enclosurename":"${list.enclosurename!''}","id":"${list.id!''}","filesize":"${list.filesize!''}","topicid":"${list.topicid}","filesize":"${list.filesize}","enclosureurl":"${list.enclosureurl}"});
</#list>
var filesLength = AttachmentList.length;
<#list blockList as list >  
	listCategory.push({"parentid":"${list.parentid!''}","id": "${list.id!''}","blockname": "${list.blockname!''}"})
</#list>  

layui.use(['form', 'laydate', 'layedit'], function(){
  form = layui.form() ;
  var layer = layui.layer; 
  var laydate = layui.laydate;
  layedit = layui.layedit;
 
	//根据选择显示
   form.on('select(selectBlock)', function(data){
	   
	   $("#selectCategory option").remove();
    	$.each(listCategory, function(index,item){
    		if(data.value == item.parentid){
    			$("#selectCategory").append("<option value='"+item.id+"'>"+item.blockname+"</option>")
    		}
    	})
    	
    	
    	if($("#selectCategory").find("option").length == 0){ //内部元素为空 重新渲染报错
    		$("#selectCategory").append("<option value=''>请选择</option>")
    	}
    	
    	form.render('select');
	}); 
	
   form.verify({ 
       content: function(value, item){ 
       	value = ue.getContent();
	      	if(value.length <= 0) {
	  	      return '请输入正文';
	  	     }
	  	  }
 	});   
//提交
  form.on('submit(formDemo)', function(data){
	$("input[name=topicFiles]").val(JSON.stringify(AttachmentList));
	$("input[name=deleteTopicFiles]").val(JSON.stringify(deleteList));
	  if( !$("input[name=hotshow]").prop("checked") ){
		  $("input[name=hot]").val("false");
	  }else{
		  $("input[name=hot]").val("true");
	  }
	  
	  if( !$("input[name=topshow]").prop("checked") ){
		  $("input[name=top]").val("false");
	  }else{
		  $("input[name=top]").val("true");
	  }
	  
	  if( !$("input[name=essenceshow]").prop("checked") ){
		  $("input[name=essence]").val("false");
	  }else{
		  $("input[name=essence]").val("true");
	  }
  }); 
   
});
var ue = UE.getEditor('content', {
	//这里可以选择自己需要的工具按钮名称
	toolbars: [
		['Source','|', 'undo', 'redo', 'removeformat', 'backcolor', 'formatmatch','|', 'italic', 'underline', 'Bold', 'test',
			'forecolor','|', 'justifyleft', 'justifycenter', 'justifyright','|','insertorderedlist' ,'|',
			'fontfamily', 'fontsize','|',
			 'simpleupload','emotion'
		]
	],
	wordCount: false,					//开启字数统计
	elementPathEnabled : false,
	retainOnlyLabelPasted : true
});

/* 加载完成后执行 */
$(document).ready(function() {
	
	$.each(listCategory, function(index,item){
		if("${topic.blockparent.id!''}" == item.parentid){
			if("${topic.block.id!''}" == item.id){
				$("#selectCategory").append("<option value='"+item.id+"'  selected>"+item.blockname+"</option>")
			}else{
				$("#selectCategory").append("<option value='"+item.id+"'>"+item.blockname+"</option>")
			}
			
		}
	})
	if($("#selectCategory").find("option").length == 0){ //内部元素为空 重新渲染报错
		$("#selectCategory").append("<option value=''>请选择</option>")
	}
	if(form){
		form.render('select');
	}
})

//上传初始化
var uploader = new plupload.Uploader({
	browse_button: 'informationUploadButton', //触发文件选择对话框的按钮，为那个元素id
	url: "${base}" + url, //服务器端的上传页面地址
	flash_swf_url: '${ctxStatic}/plupload/js//Moxie.swf', //swf文件，当需要使用swf方式进行上传时需要配置该参数
	silverlight_xap_url: '${ctxStatic}/plupload/js/Moxie.xap', //silverlight文件，当需要使用silverlight方式进行上传时需要配置该参数
	filters : {
	 	max_file_size : '40mb',
	 	prevent_duplicates: "true" //不允许选取重复文件
	},
});

//在实例对象上调用init()方法进行初始化
uploader.init();


//绑定各种事件，并在事件监听函数中做你想做的事
uploader.bind('FilesAdded',function(uploader, files) {
	$("#inforShow").removeClass("layui-hide");
	var number = 5-filesLength;
	if(uploader.files.length > number){ // 最多上传5个附件
        uploader.splice(number,999);
        layer.msg('最多上传5个附件！', {icon: 1})
    }
	$.each(files ,function(index,item){
		if($("#showAttachment li").length <= 4){ 
			$("#showAttachment").append('<li id="li-'+item.id+'"><i class="layui-icon">&#xe63d;</i><span>'+item.name+'</span></li>');
		}
	})
	uploader.start()
})

//上传后回调
uploader.bind('FileUploaded', function(uploader,UploadProgress, responseObject) {
	var obj = $.parseJSON(responseObject.response);
	$("#li-"+UploadProgress.id + " i").remove();
	$("#li-"+UploadProgress.id).prepend('<i class="layui-icon">&#xe618;</i>')
	$("#li-"+UploadProgress.id).append('<i title="删除" class="layui-icon" onclick="deleteA(this,\''+obj.fileId+'\')">&#x1006;</i>')
	var json = {};
	json.enclosurename = obj.fileName;
	json.enclosureurl = obj.filePath;
	json.enclosureid = obj.fileId;
	json.topicid = '${topic.id!''}';
	json.filesize = Math.ceil(UploadProgress.origSize / 1024);
	AttachmentList.push(json);
});

//报错
uploader.bind('Error',function(up,err) {
	if(err.code == '-600'){
		 layer.msg('文件最多可上传40M', {icon: 5});
     }else if (err.code == '-602'){
   	  layer.msg('文件重复！', {icon: 5});
     }	
	 console.log(err)	
	
});

function deleteA(i,enclosureId,fileId){
	$(i).parents("li").remove();
	if( $(i).parents("li").attr("id").split("-")[1]){
		var deleteListId = $(i).parents("li").attr("id").split("-")[1];
		uploader.removeFile(deleteListId);
	}
	
	$.each(AttachmentList, function(index,item){
		if(fileId!=null){
			if(item.id == fileId){
				deleteList.push(item)
				filesLength--;
			}
		}else{
			if(item.enclosureId == enclosureId){
				AttachmentList.splice(index, 1);
				return false;
			}
		}
	})
}
</script>